package com.example.location;

import android.graphics.Point;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.Projection;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.model.LatLngBounds;
import com.example.utils.ScreenUtil;


/**
 * @ClassName:
 * @Description:获取屏幕的经纬度范围
 * @author gongxibo
 * @date 创建时间：2015年12月21日 上午9:50:06
 */
public class GetLatlonBounsActivity extends ShowMylocationActivity {

	@Override
	protected void initListener() {

		super.initListener();
		// 地图view加载完成的回调，mBaiduMap.getProjection()写在这里面，不然报null
		mBaiduMap.setOnMapLoadedCallback(new BaiduMap.OnMapLoadedCallback() {

			@Override
			public void onMapLoaded() {
				getLatLon();

			}
		});
	}

	/**
	 * 由屏幕坐标转换为对应的经纬度，计算屏幕的经纬度范围
	 */
	protected void getLatLon() {
		// 屏幕左上角的坐标点
		Point pointLeftTOP = new Point(0, 0);
		Projection project = mBaiduMap.getProjection();
		LatLng latlongLeftTOP = project.fromScreenLocation(pointLeftTOP);
		int width = ScreenUtil.getScreenWidth(context);
		int height = ScreenUtil.getScreenHeight(context);
		System.out.println("latlong = " + latlongLeftTOP.toString()
				+ ", width = " + width + ", heigth = " + height);
		// 输出：latlong = latitude: 40.16998127733429, longitude:
		// 116.19303294874754, width = 540, heigth = 960
		// 屏幕右下角的坐标点
		Point pointRightBottom = new Point(width, height);
		// 由坐标点得到经纬度
		LatLng latlongRightBottom = mBaiduMap.getProjection()
				.fromScreenLocation(pointRightBottom);
		String bounds = "latlongLeftTOP = " + latlongLeftTOP.toString()
				+ ", latlongRightBottom = " + latlongRightBottom.toString();
		Toast.makeText(context, bounds, Toast.LENGTH_SHORT).show();
		System.out.println(bounds);
		// 输出： latlongLeftTOP = latitude: 39.92848276020053, longitude:
		// 116.39216931419946, latlongRightBottom = latitude: 39.89841818515667,
		// longitude: 116.41558813884458
		getScreenLatlon();
	}

	/**
	 * 用百度自带的方法获取屏幕经纬度范围
	 */
	private void getScreenLatlon() {
		MapStatus status = mBaiduMap.getMapStatus();
		// 得到地图中心对应屏幕坐标
		Point point = status.targetScreen;
		// 得到地图中心经纬度
		LatLng latlon = status.target;
		LatLngBounds bouns = status.bound;
		LatLng southwest = bouns.southwest;
		LatLng northeast = bouns.northeast;
		System.out.println("point= " + point + ", latlon = " + latlon
				+ " ,bounds = " + bouns.toString());
	}

	@Override
	protected void initView() {
		setTitle("查看屏幕经纬度范围");
		initMapView();
	}

}
